<template>
  <view class="container">
    <!-- 自定义搜索框 -->
    <view class="search-box" :style="{top: statusBarHeight + 'px'}">
      <view class="search-container">
        <view class="search-input">
          <uni-icons type="search" color="#999" size="20"></uni-icons>
          <input 
            type="text" 
            v-model="searchKeyword" 
            placeholder="搜索陪诊师" 
            @confirm="handleSearch"
            @input="handleInputChange"
            :focus="isSearching"
          />
          <uni-icons 
            v-if="searchKeyword" 
            type="clear" 
            size="20" 
            color="#999" 
            @click="clearSearch"
          ></uni-icons>
        </view>
        <text 
          v-if="isSearching" 
          class="cancel-btn"
          @click="cancelSearch"
        >取消</text>
      </view>
    </view>
    
    <!-- 陪诊师列表 -->
    <scroll-view 
      class="therapist-list" 
      scroll-y 
      :style="{top: statusBarHeight + 50 + 'px'}"
    >
      <view class="result-list">
        <!-- 搜索提示 -->
        <!-- <view v-if="isSearching && searchKeyword" class="search-hint">
          <text>搜索"{{searchKeyword}}"的结果 ({{displayedTherapists.length}})</text>
        </view -->

        <!-- 加载状态 -->
        <view v-if="loading" class="loading-state">
          <view class="loading-icon"></view>
          <text class="loading-text">加载中...</text>
        </view>

        <!-- 陪诊师列表 -->
        <view 
          v-for="(therapist, index) in displayedTherapists" 
          :key="therapist.id || index" 
          class="result-item therapist-item"
          @click="contactTherapist(therapist)"
        >
          <view class="therapist-info">
            <view class="therapist-header">
              <text class="therapist-name">{{ therapist.name }}</text>
              <text class="therapist-tag">专业陪诊</text>
            </view>
            <view class="therapist-details">
              <text class="gender">性别：{{ therapist.gender === '1' ? '女' : '男' }}</text>
              <text class="age" v-if="therapist.age">年龄：{{ therapist.age }}岁</text>
            </view>
            <view class="therapist-contact">
              <uni-icons type="phone" size="16" color="#1E90FF" class="phone-icon"></uni-icons>
              <text class="phone-number">{{ therapist.phone || '未提供电话号码' }}</text>
            </view>
          </view>
          <text class="iconfont icon-arrow-right arrow-right"></text>
        </view>

        <!-- 无结果提示 -->
        <view v-if="showNoResult" class="no-result">
          <uni-icons type="info" size="20" color="#999"></uni-icons>
          <text>{{ isSearching ? '没有找到"'+searchKeyword+'"相关的陪诊师' : '暂无陪诊师数据' }}</text>
        </view>
      </view>
    </scroll-view>
  </view>
</template>

<script>
import { listTherapist } from '@/api/peizhenshi.js'

export default {
  data() {
    return {
      statusBarHeight: 20,
      therapists: [],         // 原始数据
      filteredTherapists: [], // 筛选后的数据
      loading: false,
      showNoResult: false,
      searchKeyword: '',
      isSearching: false      // 是否处于搜索状态
    }
  },
  computed: {
    // 最终显示的数据
    displayedTherapists() {
      return this.isSearching ? this.filteredTherapists : this.therapists
    }
  },
  created() {
    // 获取状态栏高度
    try {
      const windowInfo = uni.getWindowInfo()
      this.statusBarHeight = windowInfo.statusBarHeight
    } catch (error) {
      console.error('获取状态栏高度失败:', error)
      this.statusBarHeight = 20
    }
    
    this.loadTherapists()
  },
  methods: {
    async loadTherapists() {
      this.loading = true
      this.showNoResult = false
      try {
        const res = await listTherapist({
          pageNum: 1,
          pageSize: 100
        })
        if (res.code === 200) {
          this.therapists = res.rows || res.data || []
          this.showNoResult = this.therapists.length === 0
        } else {
          uni.showToast({
            title: res.msg || '获取陪诊师列表失败',
            icon: 'none'
          })
          this.showNoResult = true
        }
      } catch (error) {
        console.error('获取陪诊师列表失败:', error)
        uni.showToast({
          title: '获取陪诊师列表失败',
          icon: 'none'
        })
        this.showNoResult = true
      } finally {
        this.loading = false
      }
    },
    
    contactTherapist(therapist) {
      if (!therapist.phone) {
        uni.showToast({
          title: '该陪诊师未提供电话号码',
          icon: 'none'
        })
        return
      }
      uni.makePhoneCall({
        phoneNumber: therapist.phone
      })
    },
    
    // 搜索功能
    handleSearch() {
      const keyword = this.searchKeyword.trim()
      if (!keyword) {
        this.cancelSearch()
        return
      }
      
      this.filterTherapists()
    },
    
    // 过滤陪诊师
    filterTherapists() {
      const keyword = this.searchKeyword.toLowerCase().trim()
      
      // 如果关键词为空，显示全部
      if (!keyword) {
        this.filteredTherapists = [...this.therapists]
        this.showNoResult = this.filteredTherapists.length === 0
        return
      }
      
      // 执行搜索过滤
      this.filteredTherapists = this.therapists.filter(therapist => {
        // 安全处理属性并转为字符串
        const name = therapist.name ? String(therapist.name).toLowerCase() : ''
        const phone = therapist.phone ? String(therapist.phone) : ''
        
        return name.includes(keyword) || 
               phone.includes(keyword) ||
               (therapist.gender === '1' ? '女' : '男').includes(keyword) ||
               (therapist.age && String(therapist.age).includes(keyword))
      })
      
      this.isSearching = true
      this.showNoResult = this.filteredTherapists.length === 0
    },
    
    // 清除输入框内容
    clearSearch() {
      this.searchKeyword = ''
      // 如果已经在搜索状态，刷新结果
      if (this.isSearching) {
        this.filterTherapists()
      }
    },
    
    // 取消搜索
    cancelSearch() {
      this.searchKeyword = ''
      this.isSearching = false
      this.filteredTherapists = []
      this.showNoResult = this.therapists.length === 0
    },
    
    // 输入变化处理
    handleInputChange() {
      // 为空且已搜索状态，则取消搜索
      if (!this.searchKeyword.trim() && this.isSearching) {
        this.cancelSearch()
      } else if (this.searchKeyword.trim()) {
        // 有内容时触发实时搜索
        this.isSearching = true
        this.filterTherapists()
      }
    }
  }
}
</script>

<style>
.container {
  min-height: 100vh;
  background-color: #F5F6FA;
}

/* 更新搜索栏样式 */
.search-box {
  position: fixed;
  left: 0;
  right: 0;
  height: 100rpx; /* 与参考样式一致的高度 */
  display: flex;
  align-items: center;
  padding: 10rpx 20rpx 0; /* 与参考样式一致的padding */
  background-color: #FFFFFF; /* 白色背景 */
  margin-top: -45px;
  z-index: 10;
  box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.1); /* 添加阴影 */
}

.search-container {
  display: flex;
  align-items: center;
  width: 100%;
}

.search-input {
  flex: 1;
  height: 70rpx; /* 与参考样式一致的高度 */
  background-color: #F5F6FA; /* 浅灰色背景 */
  border-radius: 35rpx; /* 较大的圆角 */
  padding: 0 30rpx;
  display: flex;
  align-items: center;
  margin-right: 20rpx;
}

.search-input input {
  flex: 1;
  height: 100%;
  font-size: 28rpx; /* 与参考样式一致的字体大小 */
  padding-left: 12rpx;
  background: transparent;
}

.cancel-btn {
  margin-left: 24rpx;
  font-size: 28rpx;
  color: #333; 
  white-space: nowrap;
}
.therapist-list {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: #F5F6FA;
    margin-top: -45px;
  top: calc(var(--status-bar-height) + 110rpx);
}

.result-list {
  padding: 32rpx;
  padding-top: 20rpx;
}

.search-hint {
  padding: 20rpx 0;
  font-size: 26rpx;
  color: #999;
  display: flex;
  align-items: center;
  justify-content: center;
  background-color: #f9f9f9;
  border-radius: 12rpx;
  margin-bottom: 24rpx;
}

.therapist-item {
  display: flex;
  align-items: center;
  padding: 28rpx;
  background-color: #FFFFFF;
  margin-bottom: 24rpx;
  border-radius: 16rpx;

  box-shadow: 0 4rpx 16rpx rgba(30, 144, 255, 0.08);
  transition: all 0.3s ease;
}

.therapist-item:active {
  transform: scale(0.98);
  background-color: #f9fbfd;
}

.therapist-info {
  flex: 1;
}

.therapist-header {
  display: flex;
  align-items: center;
  margin-bottom: 18rpx;
}

.therapist-name {
  font-size: 34rpx;
  color: #333333;
  font-weight: bold;
  margin-right: 16rpx;
  max-width: 60%;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.therapist-tag {
  font-size: 24rpx;
  color: #FFA500;
  background-color: rgba(255, 165, 0, 0.1);
  padding: 4rpx 16rpx;
  border-radius: 8rpx;
}

.therapist-details {
  display: flex;
  margin-bottom: 14rpx;
  flex-wrap: wrap;
}

.gender {
  font-size: 28rpx;
  color: #666666;
  margin-right: 32rpx;
  background: #f7f9fc;
  padding: 4rpx 16rpx;
  border-radius: 8rpx;
}

.age {
  font-size: 28rpx;
  color: #666666;
  background: #f7f9fc;
  padding: 4rpx 16rpx;
  border-radius: 8rpx;
}

.therapist-contact {
  display: flex;
  align-items: center;
  padding: 8rpx 0;
  margin-top: 10rpx;
}

.phone-icon {
  margin-right: 8rpx;
}

.phone-number {
  font-size: 28rpx;
  color: #1E90FF;
  font-weight: 500;
}

.arrow-right {
  color: #CCCCCC;
  font-size: 32rpx;
  margin-left: 20rpx;
}

.no-result {
  padding: 80rpx 0;
  text-align: center;
  color: #999999;
  font-size: 28rpx;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
}

.no-result text {
  margin-top: 20rpx;
}

.loading-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 100rpx 0;
}

.loading-icon {
  width: 60rpx;
  height: 60rpx;
  border: 4rpx solid #f3f3f3;
  border-top: 4rpx solid #1E90FF;
  border-radius: 50%;
  animation: spin 1s linear infinite;
  margin-bottom: 20rpx;
}

.loading-text {
  font-size: 28rpx;
  color: #999999;
}

@keyframes spin {
  0% { transform: rotate(0deg); }
  100% { transform: rotate(360deg); }
}
</style>